<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
$inst_id = MiscUtils::getParam('instid', 0);

$myPdo = DbUtils::createPdoInst();
$path = basename(dirname(__FILE__));

$data = new stdClass();
$data->system_id = 0;
$cond_vals = new stdClass();
$cond_vals->c = 't.instance_id = :v1';
$cond_vals->v = array(':v1' => $inst_id);
$tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'page_manage', $cond_vals, 't.id', 'ASC', NULL, NULL, NULL)->d;
if (count($tmps) > 0) {
  $data->system_id = $tmps[0]->system_id;
}

$cond_vals = new stdClass();
$cond_vals->c = 't.script != :v1';
$cond_vals->v = array(':v1' => 'admin');
$data->systems = DbUtils::get($myPdo, 'pz2_sys', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
?>
<html>
<head>
<style>
.form {
  background-color: #fff;
}
.form td {
  vertical-align: middle;
  padding: 2px 5px;
}
.form th.header {
  text-align: left;
  color: #fff;
  background-color: #e09696;
  padding-left: 5px;
  font-weight: bold;
  height: 25px;
}
.form td.title {
  background-color: #f1d0d0;
  border-top: 1px solid #fff;
  width: 150px;
}
.form td.content {
  width: 200px;
  padding: 0;
}
.form td.content input {
  width: 99%;
  border: solid 1px #ddd;
}
.form td.content select {
  width: 100%;
  border: solid 1px #ddd;
}
.form td.content textarea {
  resize: none;
  width: 99%;
  height: 70px;
  border: solid 1px #ddd;
  margin: 2px 0;
}
.list {
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
}
.list td {
  vertical-align: middle;
  text-align: left;
  padding: 2px; 
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
}
.list td.nosort {
  background-color: #dababa;
  font-weight: bold;
  
}
.list tr.none td {
  background-color: #fff;
}
.list tr.notadd td {
  background-color: #eee;
}
.list tr.curr td {
  background-color: #ff8000;
}
.imgbtn{
  background-color: #fff;
  border-bottom: 2px solid #666;
  border-top: 1px solid #666;
  border-right: 1px solid #666; 
  border-left: 1px solid #666;
  font-weight: bold;
  cursor: pointer;
  display: table-cell;
  width: 120px;
  height: 35px;
  text-align: center;
  vertical-align: middle;
  font-size: 14px;
}
.main {
  vertical-align: top;
  border-top: none;
}
.main div.content {
  background-color: #eee;
  border: 1px solid #aaa;
}
.main div.content2 {
  background-color: #fff;
  border: 1px solid #aaa;
  padding: 5px;
}
.panel {
  width: 950px;
}
.panel td.tab {
  width: 100px;
  font-size: 12px;
  font-family: Lucida Sans Unicode;
  padding: 5px 10px;
  border: 2px solid #bbb;
  background-color: #fff;
  cursor: pointer;
}
.panel td.tab:hover {
  background-color: #eee;
}
.panel td.selected {
  font-weight: bold;
  border-bottom: none;
  background-color: #eee;
}
.panel td.empty {
  border-bottom: 1px solid #aaa;
}
.image_button {
  background-color: #fff;
  border-bottom: 2px solid #666666;
  border-right: 2px solid #666666;
  border-top: 1px solid #bbbbbb;
  border-left: 1px solid #bbbbbb;
  font-weight: bold;
  cursor: pointer;
  display: inline-block;
  width: 60px;
  height: 25px;
  line-height: 25px;
  text-align: center;
  vertical-align: middle;
  color: #000;
}
.code_title {
  font-size: 14px;
  font-family: Lucida Sans Unicode;
  padding: 10px 2px 0 0;
}
.code_content {
  font-family: Helvetica, Geneva, Arial, Verdana, sans-serif;
  letter-spacing: 0.5px;
  border: none;
  width: 950px; 
  resize: vertical;
}
.mceEditor > table {
  width: 950px !important;
  height: 300px !important;
}
</style>
</head>

<body>
<table cellPadding=0 cellSpacing=0>
  <tr>
    <td style="width: 950px;" class="main">
      <div class="content2">
        <table class="form" width="320px;" cellPadding=0 cellSpacing=0>
          <tr>
            <td class="title">System</td>
            <td class="content">
              <select id="system_choose"></select>
            </td>
          </tr>
        </table>
        <div style="text-align: right; width: 320px;">
          <input type="submit" width="70px"; value="Save"  onclick="submit();"></input>
        </div>
      </div>
    </td>
  </tr>
</table>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script>
function submit() {
  var sys = document.getElementById('system_choose');

  var obj = {
    instance_id: <?php echo $inst_id; ?>,
    system_id: ((sys.options[sys.selectedIndex]._obj) ? sys.options[sys.selectedIndex]._obj.id : 0)
  };
  $.ajax({
    type: "POST",
    url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
    data: "action=submit&q=" + encodeURIComponent(JSON.stringify(obj)),
    success: function(msg){
      try {
        JSON.parse(msg);
        location.reload();
      } catch (ex) {
        alert( "Error: " + msg);
      }
    }
  });
}
//-->
</script>
<script>
  if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', init, false);
  }
  window.onload = init;

  var sys_select = document.getElementById('system_choose');
  var _system_id = JSON.parse('<?php echo addslashes(json_encode($data->system_id)); ?>');
  var _systems = JSON.parse('<?php echo addslashes(json_encode($data->systems)); ?>');
  
  function init() {
    if (!document.getElementById || !document.createElement || !arguments || arguments.callee._loaded) {
      return;
    }
    arguments.callee._loaded = true;

    sys_select.options[sys_select.options.length] = new Option('-');
    for (var i in _systems) {
      var obj = _systems[i];
      var op = new Option(obj.title);
      op._obj = obj;
      if (_system_id == obj.id) {
        op.selected = 'selected';;
      }
      sys_select.options[sys_select.options.length] = op;
    }
  }
</script>
</body>
</html>
